package com.jumi.microservice.controller.order;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.admin.*;
import com.jumi.microservice.dto.excel.*;
import com.jumi.microservice.dto.excel.ExportExcelCommonResponse;
import com.jumi.microservice.service.order.ExportExcelServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wangjingdao
 */
@RestControllerAdvice
@RequestMapping("export")
public class ExportExcelController extends BaseController {
    @Autowired
    ExportExcelServiceImpl exportExcelService;

    @ApiOperation("导出")
    @PostMapping("/exportExcel")
    public void exportExcel(ExportExcelRequest request, HttpServletResponse httpServerResponse) throws IOException {
        ExportExcelCommonResponse<ExportExcelResponse> exportExcelResponseExportExcelCommonResponse = exportExcelService.exportExcel(request);
        httpServerResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + exportExcelResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), ExportExcelResponse.class).sheet("sheet1").doWrite(exportExcelResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("导出全部订单")
    @PostMapping("/exportOrderAllExcel")
    public void exportOrderAllExcel(ExportOrderGoodsRequest request, HttpServletResponse httpServerResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponseC> exportExcelResponseExportExcelCommonResponse = exportExcelService.exportOrderAllResponse(request);
        httpServerResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + exportExcelResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), ExportOrderAllResponseC.class).sheet("sheet1").doWrite(exportExcelResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("导出预售订单")
    @PostMapping("/exportPreSaleResponse")
    public void exportPreSaleResponse(ExportPreSaleRequest request, HttpServletResponse httpServerResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponseC> exportExcelResponseExportExcelCommonResponse = exportExcelService.exportPreSaleResponse(request);
        httpServerResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServerResponse.setCharacterEncoding("utf-8");
        httpServerResponse.setHeader("Content-disposition", "attachment;filename=" + exportExcelResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServerResponse.getOutputStream(), ExportOrderAllResponseC.class).sheet("sheet1").doWrite(exportExcelResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("导出非自营代发货")
    @PostMapping("/getNonSelfOperated")
    public void getNonSelfOperated(ExportNonSelfOperatedRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponse> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.getNonSelfOperated(request,getUid(),getUserName());
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportOrderAllResponse.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("部分发货导出")
    @PostMapping("/getPartial")
    public void getPartial(ExportPartialRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponse> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.getPartial(request,getUid(),getUserName());
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportOrderAllResponse.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("待收货导出")
    @PostMapping("/getGoodsReceived")
    public void getGoodsReceived(ExportGoodsReceivedRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponseC> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.getGoodsReceived(request);
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportOrderAllResponseC.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("已完成导出")
    @PostMapping("/getCompletedList")
    public void getCompletedList(ExportCompletedRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponseC> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.getCompletedList(request);
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportOrderAllResponseC.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("售后导出")
    @PostMapping("/getAfterSale")
    public void getAfterSale(ExportCompletedRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportOrderAllResponseC> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.getAfterSale(request);
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportOrderAllResponseC.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }

    @ApiOperation("导出订单查询")
    @PostMapping("/exportFindOrder")
    public void exportFindOrder(ExportFindOrderRequest request, HttpServletResponse httpServletResponse) throws IOException {
        ExportExcelCommonResponse<ExportFindOrderResponse> exportOrderAllResponseExportExcelCommonResponse = exportExcelService.exportFindOrder(request);
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + exportOrderAllResponseExportExcelCommonResponse.getFileName());
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        EasyExcel.write(httpServletResponse.getOutputStream(), ExportFindOrderResponse.class).sheet("sheet1").doWrite(exportOrderAllResponseExportExcelCommonResponse.getData());
    }
    @ApiOperation("部分发货导入发货")
    @PostMapping("/partial/send")
    public ResponseResult<List<String>> partialSend(HttpServletRequest request) throws IOException {
        Long uid = getUid();
        String name = getUserName();
        return success(exportExcelService.excelImport(request,uid,name));
    }
    @ApiOperation("非自营待发货导入")
    @PostMapping("/selfWaitSendImport")
    public ResponseResult<List<String>> selfWaitSendImport(HttpServletRequest request) throws IOException {
        Long uid = getUid();
        String name = getUserName();
        return success(exportExcelService.excelImportNoSeal(request,uid,name));
    }
}
